<template>
  <div class="app-container">
    <!-- 标题 -->
    <div class="title">讲师列表</div>
    <!-- 表单 -->
    <el-form :inline="true" :model="teacherQuery" class="demo-form-inline">
      <el-form-item label="">
        <el-input v-model="teacherQuery.name" placeholder="讲师名"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-select v-model="teacherQuery.level" placeholder="讲师头衔">
          <el-option label="高级讲师" :value="1"></el-option>
          <el-option label="首席讲师" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          v-model="teacherQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
        >
        </el-date-picker>
        <el-date-picker
          v-model="teacherQuery.end"
          type="datetime"
          placeholder="选择截止时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getList()">
          查询
        </el-button>
        <el-button @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="60">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="name" width="80" align="center">
        <!-- <template slot-scope="scope">
          {{ scope.row.name }}
        </template> -->
      </el-table-column>
      <el-table-column label="头衔" width="100" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.level == 1 ? "高级讲师" : "首席讲师" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资历" prop="intro">
        <!-- <template slot-scope="scope">
          {{ scope.row.intro }}
        </template> -->
      </el-table-column>
      <el-table-column
        label="添加时间"
        width="150"
        align="center"
        prop="gmtCreate"
      >
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.gmtCreate }}</span>
        </template> -->
      </el-table-column>
      <el-table-column align="center" prop="sort" label="排序" width="80">
        <!-- <template slot-scope="scope">
          <span>{{ scope.row.sort }}</span>
        </template> -->
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="`/teacher/create/${scope.row.id}`">
            <el-button type="primary" size="mini" icon="el-icon-edit"
              >编辑</el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="delList(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      style="text-align: center; margin-top: 20px"
      @current-change="getList"
      :current-page="page"
      :page-size="limit"
      layout="total, prev, pager, next,jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import teacherApi from "@/api/teacher";
export default {
  data() {
    return {
      list: null, //当前列表所需要的数据
      listLoading: true, // 加载中的效果
      page: 1, // 当前页
      limit: 10, //每页记录数
      teacherQuery: {}, //条件查询对象
      total: 0, // 总记录数
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 默认page=1
    getList(page = 1) {
      // 获取当前页码
      this.page = page;

      //   调用获取讲师列表的用户api
      this.listLoading = true;
      teacherApi
        .getList(this.page, this.limit, this.teacherQuery)
        .then((res) => {
          // console.log(res);
          // 获取api中的数据
          this.list = res.data.items;

          // 获取数据中总记录数
          this.total = res.data.totalCount;
          this.limit = res.data.pageSize;

          // 当加载中停止转动
          this.listLoading = false;
        });
    },
    resetData() {
      // 清空查询条件
      this.teacherQuery = {};
      // 调用获取讲师列表的用户api
      this.getList();
    },
    delList(id) {
      // 根据id进行删除
      // console.log(id);
      // 确认是否删除
      this.$confirm("您确定要删除该讲师吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(() => {
          // 点击确认在进行调用teacher.js中的删除api
          teacherApi.delList(id).then((res) => {
            // 删除成功
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
          // 重新加载删除后的数据
        })
        .catch(() => {});
    },
  },
};
</script>

<style scoped>
.title {
  font-weight: 700;
  font-size: 22px;
  color: #9b6106;
  line-height: 48px;
}
</style>